#ifndef RSSFEED_H
#define RSSFEED_H

#include <QString>
#include <QList>
#include <QTreeWidget>
#include <QXmlStreamReader>
#include <QDomElement>
#include <QMessageBox>
#include <QDomDocument>
#include <QTextCodec>

#include "rssitem.h"
#include "uifactory.h"
#include "rssfactory.h"



class RssFeed {

    private:
        QString title;
        QString link;
        QString cat;
		RssgTypes::FeedType type;
        QByteArray encoding;
        QList<RssItem*>* msgs;
        QNetworkReply::NetworkError state;

		QString username;
		QString password;

    public:
		RssFeed(const QString &title, const QString &link, const QString &cat, RssgTypes::FeedType type = RssgTypes::FEED_UNKNOWN, const QString &enc = "UTF-8", const QNetworkReply::NetworkError &code = QNetworkReply::NoError);
        ~RssFeed() { delete msgs; }

        QString getLink() const {
            return link;
        }

        QString getCategory() const {
            return cat;
        }

        QString getTitle() const {
            return title;
        }

        QString getEncoding() const {
            return QString(encoding);
        }

        RssFeed *setEncoding(const QString &enc) {
            this->encoding = enc.toLocal8Bit();
            return this;
        }

		void setPassword(const QString &pass) {
			password = pass;
		}

		QString getPassword() const {
			return password;
		}

		void setUsername(const QString &user) {
			username = user;
		}

		QString getUsername() const {
			return username;
		}

        void setState(QNetworkReply::NetworkError code) {
            state = code;
        }

        QNetworkReply::NetworkError getState() const {
            return state;
        }

        QList<RssItem*> *getMsgs() {
            return msgs;
        }

		RssgTypes::FeedType getType() const {
            return type;
        }

        RssFeed *setTitle(const QString &title) {
            this->title = title;
            return this;
        }

        RssFeed *setLink(const QString &link) {
            this->link = link;
            return this;
        }

        RssFeed *setCategory(const QString &cat) {
            this->cat = cat;
            return this;
        }

		RssFeed *setType(const RssgTypes::FeedType &type) {
            this->type = type;
            return this;
        }

		// Hides only seen or all messages (these messages are marked as hidden and they are not displayed, but
		// can be restored).
        void hideMessages(bool onlySeen = false);

		// Trashes all (only seen) messages (cannot be restored).
		void trashMessages(bool onlySeen = false);

		// Marks all messages as not seen (they are displayed).
        void unhideMessages();

        void addMessage(const QString &title, const QString &link, const QString &description, const QString &date, bool seen = false, bool hidden = false);

        void markAllVisibleMessages(bool readUnread = true);
        bool hasUnseenMessages();

        int countOfUnseenMessages() {
            int count = 0;
            foreach (RssItem *msg, *getMsgs())
                if (!msg->isSeen() && !msg->isHidden())
                    count++;
            return count;
        }

        int countOfVisibleMessages() {
            int count = 0;
            foreach (RssItem *msg, *getMsgs())
                if (!msg->isHidden())
                    count++;
            return count;
        }

        void updateRepresentation(QTreeWidget *widget, bool append = false);

        RssItem *getItemByLink(const QString &link);

};


#endif // RSSFEED_H
